package cn.bugDance.domain.strategy.service;

import cn.bugDance.domain.award.repository.IAwardRepository;
import cn.bugDance.domain.strategy.model.entity.*;
import cn.bugDance.domain.strategy.repository.IStrategyRepository;
import cn.bugDance.domain.strategy.service.rule.chain.factory.RuleChainFactory;
import cn.bugDance.domain.strategy.service.rule.tree.factory.RuleTreeFactory;
import cn.bugDance.types.enums.ResponseCode;
import cn.bugDance.types.exception.AppException;
import org.apache.commons.lang3.StringUtils;
import org.omg.CORBA.PRIVATE_MEMBER;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 抽奖策略过程模板类
 */
public abstract class AbstractRaffleStrategyService implements IRaffleStrategyService {

    @Resource
    private RuleChainFactory ruleChainFactory;
    @Resource
    private RuleTreeFactory ruleTreeFactory;
    @Resource
    private IStrategyRepository strategyRepository;
    @Resource
    private IAwardRepository awardRepository;


    /**
     * 1.参数校验
     * @param raffleFactorEntity
     * @return
     */
    @Override
    public RaffleAwardEntity performRaffle(RaffleFactorEntity raffleFactorEntity) {
        String userId = raffleFactorEntity.getUserId();
        Long strategyId = raffleFactorEntity.getStrategyId();
        Date endDateTime = raffleFactorEntity.getEndDateTime();
        Long activityId = raffleFactorEntity.getActivityId();
        String day = raffleFactorEntity.getDay();

        if(StringUtils.isBlank(userId) || null == strategyId || null == endDateTime || null == activityId || StringUtils.isBlank(day)){
            throw new AppException(ResponseCode.ILLEGAL_PARAMETER.getCode(), ResponseCode.ILLEGAL_PARAMETER.getInfo());
        }

        RuleChainFactory.RuleChainActionEntity ruleChainActionEntity = ruleChainFactory.openRuleChain(raffleFactorEntity);

        RuleTreeFactorEntity ruleTreeFactorEntity = new RuleTreeFactorEntity();
        ruleTreeFactorEntity.setStrategyId(strategyId);
        ruleTreeFactorEntity.setAwardId(ruleChainActionEntity.getAwardId());
        ruleTreeFactorEntity.setEndDateTime(endDateTime);
        ruleTreeFactorEntity.setActivityId(activityId);
        ruleTreeFactorEntity.setDay(day);
        ruleTreeFactorEntity.setUserId(userId);

        RuleTreeFactory.RuleTreeActionEntity ruleTreeActionEntity = ruleTreeFactory.openRuleTree(ruleTreeFactorEntity);

        strategyRepository.sendAndProcessJobQueue(strategyId, ruleTreeActionEntity.getAward());
        StrategyAwardEntity strategyAwardEntity = strategyRepository.queryStrategyAwardByStrategyIdAndAwardId(strategyId, ruleTreeActionEntity.getAward());
        AwardEntity awardEntity = awardRepository.queryAwardByAwardId(ruleTreeActionEntity.getAward());

        return RaffleAwardEntity.builder()
                .awardId(ruleTreeActionEntity.getAward())
                .strategyId(strategyId)
                .awardTitle(strategyAwardEntity.getAwardTitle())
                .awardSubTitle(strategyAwardEntity.getAwardSubTitle())
                .sort(strategyAwardEntity.getSort())
                .awardKey(awardEntity.getAwardKey())
                .awardConfig(awardEntity.getAwardConfig()).build();
    }

    @Override
    public List<StrategyAwardEntity> queryStrategyAwardList(Long strategyId) {
        return strategyRepository.queryStrategyAwardByStrategyId(strategyId);
    }
}
